package com.app114.app.android.rk.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.app114.app.android.rk.conf.Conf;

/**
 * 
 * @author
 * 
 */
public class SharedPrefsUtil {

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putValue(Context context, String key, int value) {
		Editor sp = getEditor(context);
		sp.putInt(key, value);
		valueCommit(sp);
	}

	/**
	 * 
	 * @param <T>
	 * @param context
	 * @param key
	 * @param object
	 */
	public static <T> void putObject(Context context, String key, T object) {
		Editor sp = getEditor(context);
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(object);

			String objectBase64 = new String(Base64.encode(baos.toByteArray()));
			sp.putString(key, objectBase64);
			valueCommit(sp);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putValue(Context context, String key, boolean value) {
		Editor sp = getEditor(context);
		sp.putBoolean(key, value);
		valueCommit(sp);
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putValue(Context context, String key, String value) {
		Editor sp = getEditor(context);
		sp.putString(key, value);
		valueCommit(sp);
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static int getValue(Context context, String key, int defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		int value = sp.getInt(key, defValue);
		return value;
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static <T> T getObject(Context context, String key, T defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		T t = defValue;
		try {
			String personBase64 = sp.getString(key, "");
			byte[] base64Bytes = Base64.decode(personBase64);
			ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			t = (T) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static boolean getValue(Context context, String key, boolean defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		boolean value = sp.getBoolean(key, defValue);
		return value;
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static String getValue(Context context, String key, String defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		String value = sp.getString(key, defValue);
		return value;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	private static Editor getEditor(Context context) {
		return getSharedPreferences(context).edit();
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	private static SharedPreferences getSharedPreferences(Context context) {
		return context.getSharedPreferences(Conf.SF_FILE_NAME, Context.MODE_PRIVATE);
	}

	/**
	 * 
	 * @param sp
	 */
	private static void valueCommit(Editor sp) {
		sp.commit();
	}
}
